<?php
namespace app\user\controller;

use app\common\controller\Frontend;
use think\Controller;
use think\Config;
use think\Validate;
use think\captcha\Captcha as cat;
use think\Cache;
use think\Session;

class Captcha {
    public function index() {
        $captcha = new cat();
        $captcha->useArithmetic = true;
        return $captcha->entry();
    }

    public function upload(){
        $file = request()->file('file');
        $ch = curl_init();
        // API URL 和密钥
        $url = 'https://tupian.usero.cn/api/index.php';
        $token = '1c17b11693cb5ec63859b091c5b9c1b2';
        $image = $file->getInfo();
        // 构建 CURL 文件
        $cfile = new \CURLFile($image['tmp_name'], $image['type'], $image['name']);

        // POST 数据
        $data = array(
            'image' => $cfile,
            'token' => $token
        );
        // CURL 设置
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        // 执行 CURL 请求并获取响应
        $response = curl_exec($ch);
        // 错误处理
        if(curl_errno($ch)) {
            echo json_encode(array('code' => '0', 'message' => 'CURL Error: ' . curl_error($ch)));
        } else {
            // 正常返回 API 的响应数据
            echo $response;
        }
        // 关闭 CURL
        curl_close($ch);
    }
}